package com.ks.fission.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ks.core.base.OperationContext;
import com.ks.core.constant.Const;
import com.ks.core.dto.OperateBase;
import com.ks.core.dto.SearchCriteriaBase;
import com.ks.fission.entity.BFissionReward;
import com.ks.fission.dao.BFissionRewardMapper;
import com.ks.fission.entity.BFissionTask;
import com.ks.fission.service.IBFissionRewardService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 裂变-分享奖励规则 服务实现类
 * </p>
 *
 * @author kingdee123
 * @since 2020-12-21
 */
@Service
public class BFissionRewardServiceImpl extends ServiceImpl<BFissionRewardMapper, BFissionReward> implements IBFissionRewardService {

    @Resource
    private BFissionRewardMapper bFissionRewardMapper;

    @Override
    public int insertBFissionTask(OperationContext context, BFissionReward bFissionReward) {
        return bFissionRewardMapper.insert(bFissionReward);
    }

    @Override
    public int deleteBFissionReward(OperationContext context, OperateBase operateBase) {
        if(operateBase.isMultiple()){
            ArrayList<String> stringArrayList=operateBase.getIdList();
            ArrayList<Integer> integerArrayList=new ArrayList<>();
            for(String s:stringArrayList){
                integerArrayList.add(Integer.parseInt(s));
            }
            List<BFissionReward> bFissionTasks=bFissionRewardMapper.selectBatchIds(integerArrayList);
            List<Integer> status=new ArrayList<>();
            for(BFissionReward bFissionReward:bFissionTasks){
                bFissionReward.setDelMark(Const.DEL_FLAG_DEL);
                status.add(bFissionRewardMapper.updateById(bFissionReward));
            }
            if(status.contains(-Const.DEL_FLAG_DEL)){
                return Const.DEL_FLAG_NORMAL;
            }else{
                return Const.DEL_FLAG_DEL;
            }
        }else{
            Integer id=Integer.parseInt(operateBase.getIdList().get(0));
            BFissionReward bFissionReward=bFissionRewardMapper.selectById(id);
            bFissionReward.setDelMark(Const.DEL_FLAG_DEL);
            return bFissionRewardMapper.updateById(bFissionReward);
        }
    }

    @Override
    public int updateBFissionReward(OperationContext context, BFissionReward bFissionReward) {
        return bFissionRewardMapper.updateById(bFissionReward);
    }

    @Override
    public Map<String, Object> selectBFissionReward(OperationContext context, SearchCriteriaBase searchCriteriaBase) {
        ArrayList<SearchCriteriaBase.matchField> fields = searchCriteriaBase.getFields();
        EntityWrapper<BFissionReward> wrapper =new EntityWrapper<>();
        BFissionReward bFissionReward  =new BFissionReward();
        //搜索条件
        for(SearchCriteriaBase.matchField field:fields){
            if("tenantID".equals(field.getName())){
                bFissionReward.setTenantID(Integer.valueOf(field.getValue()));
            }
            if("createUserID".equals(field.getName())){
                bFissionReward.setCreateUserID(Integer.valueOf(field.getValue()));
            }
            if("createTime".equals(field.getName())){
                bFissionReward.setCreateTime(Date.valueOf(field.getValue()));
            }
        }

        bFissionReward.setOrgID(context.getOrgId().intValue());
        bFissionReward.setOrgSubID(context.getOrgSubId().intValue());
        bFissionReward.setTenantID(context.getTenantId().intValue());
        wrapper.setEntity(bFissionReward);
        if(searchCriteriaBase.getStartTime()!=null&&searchCriteriaBase.getEndTime()!=null){
            String filterSql="createTime> '"+searchCriteriaBase.getStartTime()
                    +"' and createTime<'"+searchCriteriaBase.getEndTime()+"'";
            wrapper.addFilter(filterSql);
        }
        System.out.println(1);
        RowBounds limit=new Page(searchCriteriaBase.getLimit(),searchCriteriaBase.getPageIndex());
        System.out.println(2);
        List<BFissionReward> bFissionRewards = bFissionRewardMapper.selectPage(limit,wrapper);
        Map<String, Object>  resultMap = this.isResultMap(bFissionRewards,searchCriteriaBase);
        return resultMap;
    }

    public Map<String, Object>  isResultMap(List list,SearchCriteriaBase searchCriteriaBase){
        Map<String, Object>  map = new HashMap<>();
        map.put("count",list.size());
        map.put("limit",searchCriteriaBase.getLimit());
        map.put("pageIndex",searchCriteriaBase.getPageIndex());
        map.put("hasMore",false);
        map.put("items",list);
        return  map;
    }
}
